// 频道的数据操作
import { getUserChannels, getChannels } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    },
    setAllChannelList(state, payload) {
      state.allChannelList = payload
    }
  },
  actions: {
    async getChannelListAsync(context) {
      const {
        data: { channels }
      } = await getUserChannels()
      console.log(channels)
      context.commit('setChannelList', channels)
    },
    async getAllChannelListAsync(context) {
      const {
        data: { channels }
      } = await getChannels()
      console.log(channels)
      context.commit('setAllChannelList', channels)
    }
  },
  getters: {
    // 计算属性==>需要计算出可选频道
    // 公式：所有频道数据-我的频道数据==》可选的
    optionsChannelList(state) {
      // !遍历所有的频道数组，过滤出在我的频道里面没有的数据
      return state.allChannelList.filter((item) => {
        // item是每一项{id,name}
        // const res = state.channelList.some((channel) => channel.id === item.id)
        // // console.log(res)
        // // true=> 表示该频道已经在我的频道有了
        // // false=>属于可选频道
        // if (res) {
        //   return false
        // } else {
        //   //! 符合条件才会被过滤出去
        //   return true
        // }
        return !state.channelList.some((channel) => channel.id === item.id)
      })
    }
  }
}
